package com.example.exercise.common.config;

import com.alibaba.druid.pool.DruidDataSource;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;

import javax.sql.DataSource;

@Configuration
@MapperScan(basePackages = {
        Ds1DataSourceConfig.PACKAGE1,Ds1DataSourceConfig.PACKAGE2},
        sqlSessionFactoryRef = "ds1SqlSessionFactory")
public class Ds1DataSourceConfig {

    static final String PACKAGE1 = "com.example.exercise.app.*.mapper";
    static final String PACKAGE2 = "com.example.exercise.appmanage.*.mapper";
    private static final String MAPPER_LOCATION1 = "classpath:com/example/exercise/app/*/mapper/*.xml";
    private static final String MAPPER_LOCATION2 = "classpath:com/example/exercise/appmanage/*/mapper/*.xml";

    @Value("${ds1.url}")
    private String url;

    @Value("${ds1.username}")
    private String user;

    @Value("${ds1.password}")
    private String password;

    @Value("${ds1.driver-class-name}")
    private String driverClass;

    @Bean(name = "ds1DataSource")
    @Primary
    public DataSource ds1DataSource() {
        DruidDataSource dataSource = new DruidDataSource();
        dataSource.setDriverClassName(driverClass);
        dataSource.setUrl(url);
        dataSource.setUsername(user);
        dataSource.setPassword(password);
        dataSource.setInitialSize(50);
        dataSource.setMaxActive(500);
        dataSource.setMaxWait(5000L);
        dataSource.setMinIdle(50);
        dataSource.setQueryTimeout(60);
        dataSource.setUseGlobalDataSourceStat(false);
        return dataSource;
    }

    @Bean(name = "ds1TransactionManager")
    @Primary
    public DataSourceTransactionManager ds1TransactionManager() {
        return new DataSourceTransactionManager(ds1DataSource());
    }

    @Bean(name = "ds1SqlSessionFactory")
    @Primary
    public SqlSessionFactory ds1SqlSessionFactory(@Qualifier("ds1DataSource") DataSource ds1DataSource)
            throws Exception {
        final SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean();
        sessionFactory.setDataSource(ds1DataSource);
        Resource[] resources1 = new PathMatchingResourcePatternResolver()
                .getResources(Ds1DataSourceConfig.MAPPER_LOCATION1);
        Resource[] resources2 = new PathMatchingResourcePatternResolver()
                .getResources(Ds1DataSourceConfig.MAPPER_LOCATION2);
        Resource[] resources = new Resource[resources1.length + resources2.length];
        for (int i = 0; i <  resources1.length + resources2.length; i++) {
            if(i < resources2.length) {
                resources[i] = resources1[i];
            }
            if(i >= resources1.length && i < resources1.length + resources2.length) {
                resources[i] = resources2[i - resources1.length];
            }
        }
        sessionFactory.setMapperLocations(resources);
        return sessionFactory.getObject();
    }
}